<?php

/**
 * Tools_StatsDisplayObject - Affichage des statistiques
 *
 * @package
 * @author Alexis Eric
 * @copyright Copyright (c) 2012 - Erra Prod
 * @version $1.0$
 * @access public
 */
class Tools_StatsDisplayObject extends Erra_Fields_Base_Object {

    /**
     * displayStockChart() - Affiche un diagramme temps/valeur
     * 
     * @param type $data
     * @return string
     */
    public static function displayStockChart($data) {
        $res = '';
        $res .= '<script type="text/javascript">';
        if (empty($data['chart_data'])) {
            $res .= '$(document).ready(function () {';
            $res .= "   $('#dasbhoard-content').html('Aucune information disponible pour le moment');";
            $res .= '});';
        } else {
            $res .= " window.chart = new Highcharts.StockChart({
            chart : { renderTo : 'dasbhoard-content' },
            credits: { enabled: false },
            rangeSelector : { selected : 1 },
            title : {  text : '" . addslashes($data['title']) . "' },
      rangeSelector: {
      buttonTheme: { 
            width : 70,
            height : 20
   },
   lang: {
            rangeSelectorZoom: '-'
        },
                                        
    buttons: [{
      type: 'day',
      count: 1,
      text: '1j'
    }, {
      type: 'week',
      count: 1,
      text: '1sem'
    }, {
      type: 'month',
      count: 1,
      text: '1m'
    }, {
      type: 'year',
      count: 1,
      text: '1an'
    }, {
      type: 'all',
      text: 'Tous'
    }],
    selected: 2
  },
            series : [{
                name : '" . addslashes($data['serie_name']) . "',
                data : " . str_replace('"', '', json_encode($data['chart_data'])) . ",
                tooltip: {
                    valueDecimals: 2
                }
            }]
        });";
        }
        $res .= '</script>';
        return $res;
    }

    /**
     * displayPieChart() - Affiche un camembert
     * 
     * @param type $data
     * @param type $colors
     * @return string
     */
    public static function displayPieChart($data, $colors = null) {
        $res = '';
        $res .= '<script type="text/javascript">';
        $res .= "$(function () {
    var chart;
    $(document).ready(function() {
        chart = new Highcharts.Chart({
            chart: {
                 renderTo: 'dasbhoard-content', 
                credits: { enabled: false },
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false
            },
            title: {
                text: '" . addslashes($data['title']) . "'
            },
            tooltip: {
        	    pointFormat: '{series.name}: <b>{point.percentage}%</b>',
            	percentageDecimals: 1
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        color: '#000000',
                        connectorColor: '#000000',
                        formatter: function() {
                            return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
                        }
                    }
                }
            },
            series: [{
                type: 'pie',
                name: '" . addslashes($data['chart_serie_name']) . "',
                data:  " . json_encode($data['chart_data']) . " 
            }]
        });
    });
    
});";
        $res .= '</script>';
        return $res;
    }

    /**
     * displayBarChart() - Affiche une diagramme de barres
     * 
     * @param type $data
     * @param type $color
     * @return string
     */
    public static function displayBarChart($data, $color = null, $suffix = "", array $extras = array()) {
        $color = ($color == null) ? '#B2BFFF' : $color;
        $res = '';
        $res .= '<script type="text/javascript">';


        if (empty($data['chart_data'])) {
            $res .= '$(document).ready(function () {';
            $res .= "   $('#dasbhoard-content').html('Aucune information disponible pour le moment');";
            $res .= '});';
        } else {
            $res .= "chart = new Highcharts.Chart({
            chart: { 
                     renderTo: 'dasbhoard-content', 
                     credits: { enabled: false },
                     type: 'column', 
                     margin: [ 50, 50, 100, 80] 
                   },
           title: { text: '" . addslashes($data['title']) . "' },
           xAxis: { categories: [ " . implode(',', $data['chart_x_axis']) . " ],
                             labels: { 
                                       style: { fontSize: '9px', fontFamily: 'Verdana, sans-serif' } 
                                     }
                            },
           yAxis: { min: 0,
                              title: { text: '" . $data['y_axis_title'] . "' }
                            },
           legend:  { enabled: false },
           series: [{
            name: '" . addslashes($data['chart_serie_name']) . "',
            data: [" . implode(',', $data['chart_data']) . "],
            color:'" . $color . "',
            dataLabels: {
                     enabled: true,
                     rotation: -90,
                     color: '#FFFFFF',
                     align: 'right',
                     x: -3,
                     y: 10,
                     formatter: function() {
                         return this.y+'" . $suffix . "';
                     },
                     style: {
                         fontSize: '10px',
                         fontFamily: 'Arial'
                     }
                 }
            }]";

            foreach ($extras as $extra) {
                $res .= ", $extra";
            }

            $res .= "
            });";
        }
        $res .= '</script>';
        return $res;
    }
    
    public static function displaySplineChart($data, $color = null,array $extras = array()){
        
    }
}

?>
